//
//  ClassSearchController.m
//  zhijianshejian
//
//  Created by admin on 16/6/7.
//  Copyright © 2016年 haoniu-duhuan. All rights reserved.
//

#import "ClassSearchController.h"
#import "HNConcernController.h"

@interface ClassSearchController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray * _dataSource;
    int _currentPage;
}

@end

@implementation ClassSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"搜索商品";
    
    self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor: RGB(114, 114, 114),
                                                                    UITextAttributeFont : [UIFont boldSystemFontOfSize:16]};
    [self.view setBackgroundColor:[AppConfig colorWithHexString:@"#ececec"]];
    
    UIBarButtonItem * itemLeft = [[UIBarButtonItem alloc]init];
    [itemLeft setImage:[UIImage imageNamed:@"fanhui"]];
    [itemLeft setTarget:self];
    [itemLeft setAction:@selector(backBtnClick)];
    self.navigationItem.leftBarButtonItem=itemLeft;
    
    _currentPage = 1;
    _textFiled.delegate = self;
    _dataSource = [NSMutableArray array];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//    MJRefreshGifHeader *header = [AppConfig getCustomMJRefreshGifHeader];
//    [header setRefreshingBlock:^(void){
//        [self getProductListByPage:1];
//    }];
//    self.tableView.mj_header = header;
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^(void){
//        [self getProductListByPage:_currentPage+1];
//    }];
}
- (IBAction)backBtnClick:(id)sender {
      [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    [self getProductListByPage:1];
    return YES;
}

#pragma -mark TableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_dataSource==nil||_dataSource.count==0){
        return 0;
    }
    return [_dataSource count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
    NSDictionary * d = _dataSource[indexPath.row];
    
    UIImageView * _proHeadImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 80)];
    [cell addSubview:_proHeadImg];
//    [_proHeadImg setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_BaseURL,[d objectForKey:@"logo"]]] placeholderImage:[UIImage imageNamed:@"img_def"]];
    
    UILabel * _proName = [[UILabel alloc]initWithFrame:CGRectMake(100, 5, mainScreenSize.width-110, 45)];
    [_proName setText:[d objectForKey:@"title"]];
    [_proName setTextColor:[AppConfig colorWithHexString:@"#454545"]];
    [_proName setFont:[UIFont systemFontOfSize:17]];
    [_proName setNumberOfLines:0];
    [cell addSubview:_proName];
    
    UILabel * _proPrice = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, mainScreenSize.width-110, 20)];
//      [_proPrice setText:[NSString stringWithFormat:@"¥ %0.2f",[[d objectForKey:@"price"] doubleValue]]];//正常价
    [_proPrice setText:[NSString stringWithFormat:@"¥ %0.2f",[[d objectForKey:@"salePrice"] doubleValue]]];//优惠价
    [_proPrice setTextColor:[AppConfig colorWithHexString:@"#ff5341"]];
    [_proPrice setFont:[UIFont systemFontOfSize:15]];
    [cell addSubview:_proPrice];
    
    float score = [[d objectForKey:@"score"] floatValue];
    for (int i = 0; i<5; i++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100+i*17, 77, 15, 13)];
    UIImage * image = [AppConfig composeImage:score-i withImageBack:[UIImage imageNamed:@"img_xing_nor"] withImage:[UIImage imageNamed:@"img_xing_pre"]];
        
//        UIImage * image = score>=i+1?[UIImage imageNamed:@"img_xing_pre"]:[UIImage imageNamed:@"img_xing_nor"];
        [imageView setImage:image];
        [cell addSubview:imageView];
    }
    
    //月售
    int orderAmountMonth = [[d objectForKey:@"amountMonth"] intValue];
    UILabel * _proStarRight = [[UILabel alloc]initWithFrame:CGRectMake(200, 76, 100, 14)];
    [_proStarRight setText:[NSString stringWithFormat:@"月售%d件",orderAmountMonth]];
    [_proStarRight setTextColor:[AppConfig colorWithHexString:@"#707070"]];
    [_proStarRight setFont:[UIFont systemFontOfSize:14]];
    [cell addSubview:_proStarRight];
    
    UILabel * centerLine = [[UILabel alloc]initWithFrame:CGRectMake(100, 99, mainScreenSize.width-100, 0.5)];
    [centerLine setBackgroundColor:[AppConfig colorWithHexString:@"#cacaca"]];
    [cell addSubview:centerLine];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * pro = _dataSource[indexPath.row];
//    ProductDetailController * proDetail = [[ProductDetailController alloc]init];
//    [proDetail setProductId:[pro objectForKey:@"uuid"]];
//    [self.navigationController pushViewController:proDetail animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (_dataSource == nil || _dataSource.count == 0) {
        return _tableView.frame.size.height;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, mainScreenSize.width, tableView.frame.size.height)];
    [lab setText:@"暂无数据"];
    [lab setTextColor:[AppConfig colorWithHexString:@"#707070"]];
    [lab setTextAlignment:NSTextAlignmentCenter];
    [lab setFont:[UIFont systemFontOfSize:15]];
    return lab;
}

-(void)getProductListByPage:(int)page{
    HNConcernController *concernVC = [[HNConcernController alloc]init];
    concernVC.hnTitle = self.textFiled.text;
    [self.navigationController pushViewController:concernVC animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
